package com.zjjt.dc.modules.datacatch.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.zjjt.dc.modules.datacatch.entity.QueryCondition;
import com.zjjt.dc.modules.datacatch.service.IDataCatchService;
import com.zjjt.dc.modules.datacatch.service.blh.IBLHDataCatchService;
import com.zjjt.dc.modules.datacatch.service.blh.IBLHDataDealService;
import com.zjjt.dc.modules.datacatch.service.lw.ILWDataCatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataCatchService implements IDataCatchService {


    @Autowired
    private ILWDataCatchService lwDataCatchService;

    @Autowired
    private IBLHDataCatchService blhDataCatchService;

    @Autowired
    private IBLHDataDealService iblhDataDealService;

    @Override
    public JSONObject checkDataOriginStatus() {
        return null;
    }

    @Override
    public JSONObject updateDataOriginStatus() {
        return null;
    }

    @Override
    public int updateLWWareHouseDatas(QueryCondition condition) {
        return lwDataCatchService.updateWareHouseDatas(condition);
    }

    @Override
    public int updateLWCustomerDatas(QueryCondition condition) {
        return lwDataCatchService.updateCustomerDatas(condition);
    }

    @Override
    public int updateLWOrderDatas(QueryCondition condition) {
        return lwDataCatchService.updateOrderDatas(condition);
    }

    @Override
    public int updateBLHGoodsDatas(QueryCondition condition) {
        List<String> goodsIds = null;
        int startIdx = 0;
        int limitNum = 5;

        // 从接口获取商品ID
        try {
            startIdx = condition.getStartNum() > 0 ? condition.getStartNum() : startIdx;
            limitNum = condition.getLimitNum() > 0 ? condition.getLimitNum() : limitNum;
            JSONObject data = condition.getData();
            if (ObjectUtil.isNotEmpty(data)) {
                goodsIds = data.getJSONArray("items").toList(String.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 从配置文件获取商品ID
        try {
            if (ObjectUtil.isEmpty(goodsIds)) {
                // 从配置文件获取商品ID
                JSONObject response = ResourcesJsonUtil.getResourcesJson("blh/goodsIds.json");
                JSONObject data = null;
                if (ObjectUtil.isNotEmpty(response)) {
                    data = response.getJSONObject("data");
                }
                startIdx = response.getInt("startIdx");
                limitNum = response.getInt("limitNum");
                if (ObjectUtil.isNotEmpty(data)) {
                    goodsIds = data.getJSONArray("items").toList(String.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        blhDataCatchService.updateGoodsDatas(startIdx, limitNum, goodsIds);
        return 0;
    }

    @Override
    public int updateBLHImagesDatas(QueryCondition condition) {
        List<String> goodsIds = null;
        int startIdx = 0;
        int limitNum = 5;

        // 从接口获取商品ID
        try {
            startIdx = condition.getStartNum() > 0 ? condition.getStartNum() : startIdx;
            limitNum = condition.getLimitNum() > 0 ? condition.getLimitNum() : limitNum;
            JSONObject data = condition.getData();
            if (ObjectUtil.isNotEmpty(data)) {
                goodsIds = data.getJSONArray("items").toList(String.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 从配置文件获取商品ID
        try {
            if (ObjectUtil.isEmpty(goodsIds)) {
                // 从配置文件获取商品ID
                JSONObject response = ResourcesJsonUtil.getResourcesJson("blh/goodsIds.json");
                JSONObject data = null;
                if (ObjectUtil.isNotEmpty(response)) {
                    data = response.getJSONObject("data");
                }
                startIdx = response.getInt("startIdx");
                limitNum = response.getInt("limitNum");
                if (ObjectUtil.isNotEmpty(data)) {
                    goodsIds = data.getJSONArray("items").toList(String.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        iblhDataDealService.updateGoodsImages(startIdx, limitNum, goodsIds);
        return 0;
    }
    @Override
    public int updateLWCustomerHoldings(QueryCondition condition){
        return lwDataCatchService.updateCustomerHoldings(condition);
    }


}
